package com.qkbb.admin.kuibu.qkbb.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qkbb.admin.kuibu.R;
import com.qkbb.admin.kuibu.qkbb.activity.Information;
import com.qkbb.admin.kuibu.qkbb.citylist.putCtiycode;
import com.qkbb.admin.kuibu.qkbb.funcation.HttpURLConnHelper;
import com.qkbb.admin.kuibu.qkbb.funcation.ImageDownloadSDCardCacheHelper;
import com.qkbb.admin.kuibu.qkbb.funcation.OSShelp;
import com.qkbb.admin.kuibu.qkbb.funcation.SDCardHelper;
import com.qkbb.admin.kuibu.qkbb.selectphoto.SelectPictureActivity;
import com.qkbb.admin.kuibu.qkbb.url.Url;
import com.qkbb.admin.kuibu.qkbb.view.CustomProgressDialog;
import com.qkbb.admin.kuibu.qkbb.view.TitleBarView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by admin on 2016/1/25.
 */
public class More_fragment extends Fragment {
    private ImageView imageView;
    private TextView citytext;
    private TextView idtext;
    private TextView signaturetext;
    private TextView nicknametext;
    private View view;
    private String city;
    private String strcity;
    private String id;
    private String signature;
    private String gender;
    private String age;
    private String weight;
    private String height;
    private String nickname;
    private RequestQueue queue;
    private String user_token;
    TitleBarView titleBarView;
    private String result;
    private Handler handler;
    private String imagename;
    private putCtiycode putctiycode;
    private SharedPreferences preferences;
    private CustomProgressDialog progressDialog;
    private int citycode;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_more, null);
        progressDialog=CustomProgressDialog.createDialog(getActivity());
        progressDialog.show();
        RelativeLayout layout= (RelativeLayout) view.findViewById(R.id.fragment_more_relativelayout);
        preferences=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        putctiycode = new putCtiycode();
        user_token= SDCardHelper.loadFileFromSdCard("user_token",getActivity());
        findview();
        setText();
        titleBarView.settextColor(Color.WHITE);
        titleBarView.setCenterTexiView("更多");
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what==0);
                InformationJson(result);
            }
        };
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), Information.class);
                intent.putExtra("imagename",imagename);
                intent.putExtra("nickname",nickname);
                intent.putExtra("city",strcity);
                intent.putExtra("signature",signature);
                intent.putExtra("gender",gender);
                intent.putExtra("age",age);
                intent.putExtra("weight",weight);
                intent.putExtra("height",height);
                startActivityForResult(intent,0);
            }
        });

        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode==getActivity().RESULT_OK&&requestCode==0){
            nickname=preferences.getString("nickname",nickname);
            imagename=preferences.getString("imagename",imagename);
            strcity=preferences.getString("city", city);


            Log.e("onActivityResult","onActivityResult");
            Log.e("data.getStringExtr",data.getStringExtra("nickname"));
            Log.e("strcity",strcity);
            Log.e("sigmature",signature);
            String url = new OSShelp(getActivity()).getBitmap(imagename);
            new ImageDownloadSDCardCacheHelper().myDownloadImageSDCardCache(getActivity(),url,imageView,imagename,1);
            citytext.setText(strcity);
            nicknametext.setText(nickname);
            signaturetext.setText(signature);
        }
    }

    public void findview(){
        imageView = (ImageView) view.findViewById(R.id.fragment_more_image);
        citytext= (TextView) view.findViewById(R.id.fragment_more_region);
        idtext= (TextView) view.findViewById(R.id.fragment_more_id);
        signaturetext= (TextView) view.findViewById(R.id.fragment_more_signature);
        nicknametext= (TextView) view.findViewById(R.id.fragment_more_name);
        titleBarView= (TitleBarView) view.findViewById(R.id.fragment_more_titlebar);
        queue= Volley.newRequestQueue(getActivity());
    }

    public void setText(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                byte b[]=HttpURLConnHelper.loadByteFromURL(Url.QUERYINFORMATION + user_token,getActivity());
                if (b!=null){
                    result=new String(b);
                    Log.e("result",result);
                }
                Message message=new Message();
                message.what=0;
                handler.sendMessage(message);

            }
        }).start();


//        HttpUtils httpUtils=new HttpUtils();
//        httpUtils.send(HttpRequest.HttpMethod.GET, Url.QUERYINFORMATION + user_token, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                Log.e("chaxun", responseInfo.result);
//                InformationJson(responseInfo);
//
//            }
//
//            @Override
//            public void onFailure(HttpException e, String s) {
//
//            }
//        });

//        StringRequest request=new StringRequest(Url.QUERYINFORMATION + user_token, new Response.Listener<String>() {
//
//            @Override
//            public void onResponse(String s) {
//               Log.e("s",s);
//
//                try {
//                    JSONObject jsonobject=new JSONObject(s);
//                    JSONObject jsonobject2=jsonobject.getJSONObject("data");
//                    JSONObject jsonobject3=jsonobject2.getJSONObject("User");
//                    id=jsonobject3.getString("userid");
//                    nickname=jsonobject3.getString("nickname");
//                    sigmature=jsonobject3.getString("signature");
//                    city=jsonobject3.getString("cityid");
//
//                    idtext.setText(id);
//                    citytext.setText(city);
//                    nicknametext.setText(nickname);
//                    signaturetext.setText(sigmature);
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError volleyError) {
//
//            }
//        });
//        queue.add(request);
    }

    private void InformationJson(String result) {
        if (result!=null) {


            try {
                JSONObject jsonobject = new JSONObject(result);
                JSONObject jsonobject2 = jsonobject.getJSONObject("data");
                JSONObject jsonobject3 = jsonobject2.getJSONObject("User");
                id = jsonobject3.getString("userid");
                nickname = jsonobject3.getString("nickname");
                signature = jsonobject3.getString("signature");
                city = jsonobject3.getString("cityid");
                String url = new OSShelp(getActivity()).getBitmap(jsonobject3.getString("photo"));
                imagename = jsonobject3.getString("photo");
                SDCardHelper.saveStringToSDCardCustomDir(imagename,"imagename");
                age=jsonobject3.getString("age");
                weight=jsonobject3.getString("weight");
                height=jsonobject3.getString("height");
                gender=jsonobject3.getString("gender");
                new ImageDownloadSDCardCacheHelper().myDownloadImageSDCardCache(getActivity(), url, imageView, imagename, 1);
                Log.e("city", city + "");
                idtext.setText(id);
                if (!city.equals("null")) {
                     citycode= new Integer(city);
                }else {
                    citycode=1059;
                }
                strcity=putctiycode.Getcitynames(citycode, getActivity());
                citytext.setText(strcity);
                nicknametext.setText(nickname);
                if (!signature.equals("null")){
                    signaturetext.setText(signature);
                }
                progressDialog.cancel();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public void onClick(View view){
        switch (view.getId()){
            case R.id.fragment_more_photos:
                break;
            case R.id.fragment_more_reward:
                break;
            case R.id.fragment_more_notification:
                break;
            case R.id.fragment_more_wallet:
                break;
            case R.id.fragment_more_history:
                break;
            case R.id.fragment_more_shop:
                break;
            case R.id.fragment_more_hardware:
                break;

        }

    }
}
